package com.wj.opratorlog.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

public class TestMain {
                public static void main(String[] args) {
                    char[] inputs = {'1', '-', '2', '*', '3'};
                    int calculate = calculate(inputs);
                    System.out.println("11111--------------------:"+calculate);




                }
                public static int calculate(char[] inputs) {
                    Stack<Integer> operands = new Stack<>();
                    Stack<Character> operators = new Stack<>();

                    for (char c : inputs) {
                        if (Character.isDigit(c)) {
                            operands.push(Character.getNumericValue(c));
                        } else if (isValidOperator(c)) {
                            while (!operators.isEmpty() && hasPrecedence(c, operators.peek())) {
                                resultOperation(operands, operators.pop());
                            }
                            operators.push(c);
                        }
                    }

                    while (!operators.isEmpty()) {
                        resultOperation(operands, operators.pop());
                    }

                    return operands.peek();
                }

                private static boolean isValidOperator(char c) {
                    return c == '+' || c == '-' || c == '*' || c == '/';
                }

                private static boolean hasPrecedence(char op1, char op2) {
                    // 这里简化了优先级规则，假设乘除优先级高于加减
                    if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) {
                        return false;
                    }
                    return true;
                }

                private static void resultOperation(Stack<Integer> operands, char operator) {
                    int operand2 = operands.pop();
                    int operand1 = operands.pop();

                    switch (operator) {
                        case '+':
                            operands.push(operand1 + operand2);
                            break;
                        case '-':
                            operands.push(operand1 - operand2);
                            break;
                        case '*':
                            operands.push(operand1 * operand2);
                            break;
                        case '/':
                            // 注意，这里假设不会出现除数为零的情况
                            operands.push(operand1 / operand2);
                            break;
                    }
                }



                void  quicksort(Integer[] arr , int left , int right){



                    int i = left , j = right , temp = arr[left];
                    if(left < right){
                        while(i < j){
                            while(i < j && arr[j] >= temp)
                                j--;
                            if(i < j)
                                arr[i++] = arr[j];
                            while(i < j && arr[i] < temp)
                                i++;
                            if(i < j)
                                arr[j--] =arr[i];
                            }
                        }

                    arr[i] = temp;
                    arr[j]=arr[i];
                    quicksort(arr,left,i-1);
                    quicksort(arr,i+1,right);



                 }
            }


